// copied from the War.groovy

grailsHome = Ant.project.properties."environment.GRAILS_HOME"

includeTargets << new File ( "${grailsHome}/scripts/Init.groovy" ) 
includeTargets << new File ( "${grailsHome}/scripts/War.groovy" )  

target('default': "The description of the script goes here!") {
    explodedWar()
}

target(explodedWar: "The implementation task") {

	depends( clean,  packageApp)
	 
	try {
        stagingDir = "${basedir}/explodedWar/${grailsAppName}"		

            def fileName = grailsAppName	
            def version = Ant.antProject.properties.'app.version'
            if (version) {
                version = '-'+version
            } else {
                version = ''
            }
            warName = "${basedir}/${fileName}${version}.war"

        Ant.mkdir(dir:stagingDir)

		Ant.copy(preservelastmodified: true, todir:stagingDir, overwrite:true) {
			fileset(dir:"${basedir}/web-app", includes:"**") 
		}       
		Ant.copy(preservelastmodified: true, todir:"${stagingDir}/WEB-INF/grails-app", overwrite:true) {
			fileset(dir:"${basedir}/grails-app", includes:"views/**")
            fileset(dir:"${resourcesDirPath}/grails-app", includes:"i18n/**")
        }
		Ant.copy(preservelastmodified: true, todir:"${stagingDir}/WEB-INF/classes") {
            fileset(dir:classesDirPath) {
				exclude(name:"hibernate")
				exclude(name:"spring")	
				exclude(name:"hibernate/*")
				exclude(name:"spring/*")
			}
        }

        Ant.mkdir(dir:"${stagingDir}/WEB-INF/spring")
        Ant.copy(preservelastmodified: true, todir:"${stagingDir}/WEB-INF/spring") {
            fileset(dir:"${basedir}/grails-app/conf/spring", includes:"**/*.xml")            
        }

        Ant.copy(preservelastmodified: true, todir:"${stagingDir}/WEB-INF/classes", failonerror:false) {
            fileset(dir:"${basedir}/grails-app/conf") {
				exclude(name:"*.groovy")
				exclude(name:"log4j.*")
				exclude(name:"**/hibernate/**")
				exclude(name:"**/spring/**")
			}
            fileset(dir:"${basedir}/grails-app/conf/hibernate", includes:"**/**")
            fileset(dir:"${basedir}/src/java") {
                include(name:"**/**")
                exclude(name:"**/*.java")
            }
            fileset(dir:"${resourcesDirPath}", includes:"log4j.properties")
        }
		              
		scaffoldDir = "${stagingDir}/WEB-INF/templates/scaffolding"
		packageTemplates()

		Ant.copy(preservelastmodified: true, todir:"${stagingDir}/WEB-INF/lib") {
			fileset(dir:"${grailsHome}/dist") {
					include(name:"grails-*.jar")
			}
			fileset(dir:"${basedir}/lib") {
					include(name:"*.jar")
			}
			if(config.grails.war.dependencies instanceof Closure) {
				def fileset = config.grails.war.dependencies
				fileset.delegate = delegate
				fileset.resolveStrategy = Closure.DELEGATE_FIRST
				fileset()
			}
			else {
	            fileset(dir:"${grailsHome}/lib") {
                    def deps = config.grails.war.dependencies ?: DEFAULT_DEPS
                    for(d in deps) {
	                    include(name:d)
	                }
	                if(antProject.properties."ant.java.version" != "1.4") {
                        deps = config.grails.war.java5.dependencies ?: DEFAULT_J5_DEPS
                        for(d in deps) {
	                        include(name:d)
	                    }
	                }
	            }
	            if(antProject.properties."ant.java.version" == "1.4") {
	                fileset(dir:"${grailsHome}/lib/endorsed") {
	                        include(name:"*.jar")
	                }
	            }				
			}
		}
        Ant.copy(preservelastmodified: true, file:webXmlFile.absolutePath, tofile:"${stagingDir}/WEB-INF/web.xml")
        Ant.delete(file:webXmlFile)
        Ant.copy(preservelastmodified: true, todir:"${stagingDir}/WEB-INF/lib", flatten:true, failonerror:false) {
			fileset(dir:"${basedir}/plugins") {
                include(name:"*/lib/*.jar")
            }
        }
		  
	    Ant.propertyfile(file:"${stagingDir}/WEB-INF/classes/application.properties") {
	        entry(key:"grails.env", value:grailsEnv)
	        entry(key:"grails.war.deployed", value:"true")
	    }		
		
		Ant.replace(file:"${stagingDir}/WEB-INF/applicationContext.xml",
				token:"classpath*:", value:"" )
				
	    if(config.grails.war.resources) {
			def callable = config.grails.war.resources
			callable.delegate = Ant
			callable.resolveStrategy = Closure.DELEGATE_FIRST
			callable(stagingDir)
		}

		warPlugins()
		createDescriptor()
	}   
	finally {
	}
}                                                                    

